package com.syaccp.utils;


import io.swagger.annotations.ApiModel;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author Lyh
 * @since 2023/7/15
 */
@Component
@ApiModel(value = "DateUtils类", description = "日期工具类")
@SuppressWarnings("all")
public class DateUtils {

    static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    static SimpleDateFormat noConnectorDateFormat = new SimpleDateFormat("yyyyMMdd");

    /**
     * 获得当日格式字符串
     */
    public static String getTodayDateTime() {
        return dateTimeFormat.format(new Date());
    }

    /**
     * 获得当日格式字符串
     */
    public static String getNoConnectorDateFormat() {
        return noConnectorDateFormat.format(new Date());
    }

    /**
     * 返回LocalDateTime类型当前格式日期
     */
    public static LocalDateTime getTodayLocalDateTime() {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        return LocalDateTime.parse(fmt.format(localDateTime), fmt);
    }

    /**
     * 获得当日00:00:00格式字符串
     */
    public static String getTodayDateTimeFirst() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return dateTimeFormat.format(cal.getTime());
    }

    /**
     * 获得当日23:59:59格式字符串
     */
    public static String getTodayDateTimeLast() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return dateTimeFormat.format(cal.getTime());
    }

    /**
     * 获得上个月00:00:00格式字符串
     */
    public static String getMonthBeginDateTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return dateTimeFormat.format(cal.getTime());
    }


    public static Date getTodayDateObjDateTime(){
        Date time=null;
        try {
            time= dateTimeFormat.parse(dateTimeFormat.format(new Date()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return time;
    }
}
